package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.data.R;
import com.dk.enums.ResultEnum;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.User;
import com.dongdongshop.pojo.common;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.service.Tborderservice;
import com.dongdongshop.vo.AddressVo;
import com.dongdongshop.vo.CartVO;
import com.dongdongshop.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


//订单解决页面
@Controller
@RequestMapping("order")
public class OrderController {


    @DubboReference
    TbAddressService ts;//地址信息

    @Autowired
    RedisTemplate redisTemplate;

    @DubboReference(timeout = 3333333)
    Tborderservice tos;//购物车商家表

    @Autowired
    RocketMQTemplate rocketMQTemplate;


    @Autowired
    private MongoTemplate mongoTemplate;


    //订单解决页面
    //跳转
    @RequestMapping("getOrder")
    @ResponseBody
    public R getOrder(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();//判断是否登录
        if(user==null){
            return R.response(ResultEnum.USER_LOGINERROR);
        }else{
            return R.ok().setData(user);
        }
    }



    //跳转页面订单页面
    @RequestMapping("togo")
    public String togo(){
        return "getOrderInfo";
    }

    //查询收货地址
    @RequestMapping("getAddress")
    @ResponseBody
    public R<List<TbAddress>> getAddress(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();//判断是否登录
        String username = user.getUsername();
//        List<TbAddress> addls = ts.getAddress(username);
        List<AddressVo> addVo = ts.getaddVo(username);//封装的vo类

        Long id = user.getId();
        List<CartVO> cartlist = (List<CartVO>) redisTemplate.boundHashOps("cartlist").get(id);

        OrderVO ovo=new OrderVO();
        ovo.setTbList(addVo);
        ovo.setCalist(cartlist);
        return R.ok().setData(ovo);
    }



    //增加购物车商品
    @RequestMapping("addorder")
    @ResponseBody
    public R addorder(Long userid,String strs){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long id = user.getId();

        Map<String, Object> map = tos.addorder(userid, strs, id);
        JSONObject productMapStr = (JSONObject) JSONObject.toJSON(map);
        return R.ok().setData(productMapStr);
    }



    //查询订单信息
    @RequestMapping("getallSend")
    @ResponseBody
    public R<List<TbOrder>>getallSend(){
        List<TbOrder> ls = tos.getallSend();
        return R.ok().setData(ls);
    }


    //退款，修改订单状态
    @RequestMapping("upstauts")
    @ResponseBody
    public R upstauts(Long order){
         tos.upstauts(order);
        return R.ok();
    }





    //查询评论
    @RequestMapping("selectComment")
    @CrossOrigin//跨域
    @ResponseBody
    public R<List<common>> selectComment(){
        List<common> ls = mongoTemplate.findAll(common.class);
        return R.ok().setData(ls);

    }


    //手动增加评论
    @RequestMapping("insertComment")
    @ResponseBody
    @CrossOrigin
    public R insertComment(){
        try {
            Date date=new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat();
            String format = simpleDateFormat.format(date);
            common com=new common("2","李四","111","还行错","good好","111","视频",10,format,"2");
            int i = tos.insertComment(com);
            if (i == 0){
                return R.error();
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }







}
